package wwl.lsf.hellospringboot.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wwl.lsf.hellospringboot.service.ArthasServiceImpl;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Title
 * @Author wangwenliang
 * @Date 2020/8/22
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("")
@Data
public class ArthasController {

    private volatile boolean flag;

    private Lock lock = new ReentrantLock();
    private Lock lock2 = new ReentrantLock();

    @Autowired
    private ArthasServiceImpl arthasService;

    private static ArthasServiceImpl arthasServicestat = new ArthasServiceImpl();

    @RequestMapping
    public String hello() {
        return "hello";
    }

    @RequestMapping("/sum/{a}/{b}")
    public String sum(@PathVariable("a") Integer a, @PathVariable("b") Integer b) {
        return arthasService.sum(a, b);
    }
    @RequestMapping("/div/{a}/{b}")
    public String div(@PathVariable("a") Integer a, @PathVariable("b") Integer b) {
        return arthasService.div(a, b);
    }

    @RequestMapping("/openWhileTrue")
    public String openWhileTrue() {
        //死循环
        while (flag) {
            System.out.println("毛驴拉磨,走不出耐个圈");
        }
        return "whileTrue";
    }

    @RequestMapping("/falseFlag")
    public String falseFlag() {
        flag = false;
        return "falseFlag";
    }

    @RequestMapping("/trueFlag")
    public String trueFlag() {
        flag = true;
        return "trueFlag";
    }

    @RequestMapping("/lock")
    public String lock() throws InterruptedException {
        lock.lock();
        try {
            log.error("我要开始阻塞了");
            while (flag) {
                Thread.sleep(1000);
            }
        } finally {
            lock.unlock();
        }
        return "lock";
    }

    @RequestMapping("/lock2")
    public String lock2() throws InterruptedException {
        {
            lock2.lock();
            try {
                log.error("2222222222222我要开始阻塞了");
                while (flag) {
                    Thread.sleep(1000);
                }
            } finally {
                lock2.unlock();
            }
            return "lock2";
        }
    }

    @RequestMapping("/log/{logInfo}")
    public String log(@PathVariable("logInfo") String logInfo) {
        log.debug("hello:{},我是DEBUG级别", logInfo);
        log.info("hello:{},我是INFO级别", logInfo);
        log.error("hello:{},我是ERROR级别", logInfo);
        return logInfo;
    }

    @RequestMapping("/trace/{sleepTime}")
    public String trace(@PathVariable("sleepTime") Integer sleepTime) {
        long startTime = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();


        try {
            Thread.sleep(sleepTime * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        arthasService.traceService(sleepTime);

        long endTime = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();


        return String.valueOf(endTime - startTime);
    }


}
